﻿namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System;
    using System.ComponentModel;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// A style that also contains left and right properties
    /// </summary>
    public class FormStyle : Style
    {
        /// <summary>
        /// Gets or sets Left position.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        /// <value>
        /// The left position.
        /// </value>
        [NotifyParentProperty(true)]
        [Category("Layout")]
        [DefaultValue(typeof(Unit), "")]
        public Unit Left
        {
            get
            {
                return ViewState.GetValue("Left", Unit.Empty);                
            }

            set
            {
                if (value.Value < 0.0)
                {
                    throw new ArgumentOutOfRangeException("value", "Invalid Left");
                }

                ViewState["Left"] = value;                
            }
        }

        /// <summary>
        /// Gets or sets Right position.
        /// </summary>
        /// <exception cref="ArgumentOutOfRangeException">
        /// </exception>
        /// <value>
        /// The right position.
        /// </value>
        [NotifyParentProperty(true)]
        [Category("Layout")]
        [DefaultValue(typeof(Unit), "")]
        public Unit Right
        {
            get
            {
                return ViewState.GetValue("Right", Unit.Empty);                
            }

            set
            {
                if (value.Value < 0.0)
                {
                    throw new ArgumentOutOfRangeException("value", "Invalid Right");
                }

                ViewState["Right"] = value;                
            }
        }

        /// <summary>
        /// adds right and left positions to the style
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public override void AddAttributesToRender(HtmlTextWriter writer, WebControl owner)
        {
            base.AddAttributesToRender(writer, owner);
            if (!Right.IsEmpty)
            {
                writer.AddStyleAttribute("right", Right.ToString());
            }

            if (!Left.IsEmpty)
            {
                writer.AddStyleAttribute("left", Left.ToString());
            }
        }

        /// <summary>
        /// fills style attributes
        /// </summary>
        /// <param name="attributes">
        /// The attributes.
        /// </param>
        /// <param name="urlResolver">
        /// The url resolver.
        /// </param>
        protected override void FillStyleAttributes(CssStyleCollection attributes, IUrlResolutionService urlResolver)
        {
            base.FillStyleAttributes(attributes, urlResolver);
            if (!Right.IsEmpty)
            {
                attributes.Add("right", Right.ToString());
            }

            if (!Left.IsEmpty)
            {
                attributes.Add("left", Left.ToString());
            }
        }
    }
}